import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";
import { request } from "../../utils";

const channelStore = createSlice({
  name: "channel",
  initialState: {
    channelList: [],
  },
  reducers: {
    setChannels: (state, action) => {
      state.channelList = action.payload;
    },
  },
});

const { setChannels } = channelStore.actions;
const fetchChannelList = () => {
  return async (dispatch) => {
    const res = await request.get("/api/channels");
    console.log(res.data);
    dispatch(setChannels(res.data));
  };
};

export { fetchChannelList };
const channelReducer = channelStore.reducer;
export default channelReducer;